
#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //创建对象
    tetrisBox =new tetrisbox;
    nextTetrisBox =new nexttetrisbox;
    nextTetrislabel =new QLabel(tr("下一个："));
    controllabel =new QLabel(tr("控制："));
    w_controllabel =new QLabel(tr("w-旋转"));
    s_controllabel =new QLabel(tr("S-向下移动"));
    a_controllabel =new QLabel(tr("A-向左移动·"));
    d_controllabel =new QLabel(tr("D-向右移动"));
    h_controllabel =new QLabel(tr("H-开始"));
    j_controllabel =new QLabel(tr("J-暂停"));
    c_controllabel =new QLabel(tr("C-重新开始"));
    m_controllabel =new QLabel(tr("M-结束游戏"));
    scoreTitlelabel=new QLabel(tr("得分："));
    scorelabel=new QLabel(tr("0"));
    mainLayout=new QGridLayout;
    //设置mainlayout的水平和横向的间隔为20
    mainLayout->setHorizontalSpacing(20);
    mainLayout->setVerticalSpacing(20);
    //设置mainlayout居中
    mainLayout->setAlignment(Qt::AlignCenter);
    //添加各个widget
    mainLayout->addWidget(tetrisBox,0,0,14,1);
    mainLayout->addWidget(nextTetrislabel,0,1);
    mainLayout->addWidget(nextTetrisBox,1,1,1,2);
    mainLayout->addWidget(controllabel,5,1);
    mainLayout->addWidget(w_controllabel,6,1);
    mainLayout->addWidget(s_controllabel,6,2);
    mainLayout->addWidget(a_controllabel,7,1);
    mainLayout->addWidget(d_controllabel,7,2);
    mainLayout->addWidget(h_controllabel,8,1);
    mainLayout->addWidget(j_controllabel,8,2);
    mainLayout->addWidget(c_controllabel,9,1);
    mainLayout->addWidget(m_controllabel,9,2);
    mainLayout->addWidget(scoreTitlelabel,12,1);
    mainLayout->addWidget(scorelabel,12,2);

    //因为mainwindow已有一个layout，所以不能直接将mainlayout
    //设置到mainwidows中，需要先将mainlayout设置为一个widget的layout
    //再将widget设置为mainlayout的centralwidget
    QWidget *widget=new QWidget(this);
    widget->setLayout(mainLayout);
    setCentralWidget(widget);
    //设置背景窗口为灰色
    setPalette(Qt::darkBlue);
    //设置窗口位置居中
   /* QDesktopWidget *desktopWidget =QApplication::desktop();
      int w=(desktopWidget->width()-this->width())/2;
     int h=s;
     move(w,h);*/

    //初始化
    status =STATUS_OFF;
    nextTetrisBox->updateNextTetris(tetris);
    setWindowTitle(tr("Game_Tetris - OFF"));
    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(onTimer()));

}

MainWindow::~MainWindow(){};

//相应键盘事件
void MainWindow::keyPressEvent(QKeyEvent *event){
    //w键-进行旋转并更新游戏窗口
    if(event->key()==Qt::Key_W){
        if (tetris.rotate()){
            //需要游戏状态：正常运行
            if (status==STATUS_ON){
                tetrisBox->updateTetris(tetris);
            }
        }
    }
    //A键-将方块向左移动并更新游戏窗口内容
    else if (event->key()==Qt::Key_A){
        //需要游戏状态：正常运行
        if (status==STATUS_ON){
            if(tetris.moveToLeft()){
                tetrisBox->updateTetris(tetris);
            }
        }
    }
    //S键-将方块向下移动并更新游戏窗口内容
    else if (event->key()==Qt::Key_S){
        //需要游戏状态：正常运行
        if (status==STATUS_ON){
            if(tetris.moveToBottom()){
                tetrisBox->updateTetris(tetris);
                nextTetrisBox->updateNextTetris(tetris);
                updateScore();
            }
            else  //游戏结束
            {//计时器停止
                timer->stop();
                //输出结束提示
                QString str;
                str+=QString("Game Over!\nyour Score is: %1!").arg(tetris.getScore());
                QMessageBox::information(this,tr("Game Over"),str);
                //更改游戏状态：游戏结束
                status=STATUS_END;
                setWindowTitle(tr("Game_Tetris - END"));
            }
        }
    }
    //D键-将方块向右移动并更新游戏窗口内容
    else if (event->key()==Qt::Key_D){
        //需要游戏状态：正常运行
        if (status==STATUS_ON){
            if(tetris.moveToRight()){
                tetrisBox->updateTetris(tetris);
            }
        }
    }
    //H键-开始游戏
    //不同状态的相应
    //之前状态    之后状态
    //游戏暂停--> 正常运行
    //还未开始--> 正常运行
    //游戏结束--> 正常运行
    else if(event ->key()==Qt::Key_H){
        if(status==STATUS_PAUSE){
            timer->start(500);
            status=STATUS_ON;
            setWindowTitle(tr("Game_Trertris - ON"));

        }
        else if(status ==STATUS_OFF){
            //初始化窗口视图
            tetris.createBlock();
            tetrisBox->updateTetris(tetris);
            nextTetrisBox->updateNextTetris(tetris);
            updateScore();

            status =STATUS_ON;

           setWindowTitle(tr("Game_Tetris - ON"));
            timer->start(500);

        }
        else if(status ==STATUS_END){
            //初始化tetris
            tetris.clear();
            tetris.createBlock();
            tetrisBox->updateTetris(tetris);
            nextTetrisBox->updateNextTetris(tetris);
            updateScore();

            status=STATUS_ON;
            setWindowTitle(tr("Game_Tetris - ON"));
            timer->start(500);
        }
    }
    //J键暂停
    else if(event->key()==Qt::Key_J){
        //需要游戏状态为：正常运行
        if(status==STATUS_ON){
            timer->stop();
            status=STATUS_PAUSE;
            setWindowTitle(tr("Game_Tetris - PAUSE"));
        }
    }
    //C键-重新开始
    else if(event->key()==Qt::Key_C){
        timer->stop();
        tetris.clear();
        tetrisBox->updateTetris(tetris);
        nextTetrisBox->updateNextTetris(tetris);
        updateScore();
        status = STATUS_OFF;
        setWindowTitle(tr("Game_Tetris-OFF"));
    }
    //M键-关闭游戏
    else if(event->key()==Qt::Key_M){
        close();
    }

}

void MainWindow::onTimer(){
    if(tetris.moveToBottom()){
    tetrisBox->updateTetris(tetris);
    nextTetrisBox->updateNextTetris(tetris);
    updateScore();
    }
    else{
    timer->stop();
    QString str;
    str += QString("Game Over!\nYour Score is: %1!").arg(tetris.getScore());
    QMessageBox::information(this,tr("Game Over"),str);
    status =STATUS_END;
    setWindowTitle(tr("Game_Tetris - END"));
    }
}

void MainWindow::updateScore(){
    QString str;
    int score= tetris.getScore();
    str += QString("%1").arg(score);
    scorelabel->setText(str);
}

//若窗口最小化就停止计时器
void MainWindow::changeEvent(QEvent *event){
    if(event->type() != QEvent::WindowStateChange){
    return;
    }
    if (windowState()==Qt::WindowMinimized){
    timer->stop();
    }
}
